package com.heima.wemedia.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.NewsAuthDto;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsDto2;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.vos.WmNewsVo;
import com.heima.wemedia.service.WmNewsService;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/v1/news")
@Slf4j
public class WmNewsController {
    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 查询文章
     * @param dto 查询参数
     */
    @PostMapping("/list")
    public ResponseResult<List<WmNews>> findAll(@RequestBody WmNewsPageReqDto dto){
        return wmNewsService.findAll(dto);
    }

    /**
     * 发布文章或保存草稿
     * @param dto 文章信息
     */
    @PostMapping("/submit")
    public ResponseResult<AppHttpCodeEnum> submitNews(@RequestBody WmNewsDto dto)
            throws IOException, TesseractException {
        return wmNewsService.submitOrSaveDraftNews(dto);
    }

    /**
     * 查询文章详情
     * @param id 文章id
     */
    @GetMapping("/one/{id}")
    public ResponseResult<WmNews> findOne(@PathVariable Integer id) {
        return wmNewsService.queryById(id);
    }

    /**
     * 删除文章
     * @param id 文章id
     */
    @GetMapping("/del_news/{id}")
    public ResponseResult<String> delNews(@PathVariable Integer id) {
        return wmNewsService.deleteById(id);
    }

    /**
     * 上下架文章
     * @param dto 上下架参数
     */
    @PostMapping("/down_or_up")
    public ResponseResult<String> downOrUp(@RequestBody WmNewsDto2 dto) {
        return wmNewsService.downOrUp(dto);
    }


    /**
     * 查询文章列表
     * @param dto 查询参数
     */
    @PostMapping("/list_vo")
    public ResponseResult<List<WmNewsVo>> listVo(@RequestBody NewsAuthDto dto) {
        return wmNewsService.listVo(dto);
    }

    /**
     * 查询文章详情
     * @param id 文章id
     */
    @GetMapping("/one_vo/{id}")
    public ResponseResult<WmNewsVo> findOneVo(@PathVariable Integer id) {
        return wmNewsService.findWmNewsVo(id);
    }

    /**
     * 审核失败
     * @param dto 审核参数
     */
    @PostMapping("/auth_fail")
    public ResponseResult<AppHttpCodeEnum> authFail(@RequestBody NewsAuthDto dto) {
        return wmNewsService.authWmNews(dto, WmNews.Status.FAIL.getCode());
    }

    /**
     * 审核成功
     * @param dto 审核参数
     */
    @PostMapping("/auth_pass")
    public ResponseResult<AppHttpCodeEnum> authPass(@RequestBody NewsAuthDto dto) {
        return wmNewsService.authWmNews(dto, WmNews.Status.ADMIN_SUCCESS.getCode());
    }

    @ExceptionHandler(JsonProcessingException.class)
    public ResponseResult<AppHttpCodeEnum> jsonProcessingExceptionHandler(JsonProcessingException e) {
        log.error("jsonProcessingExceptionHandler error:{}", e.getMessage());
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

}
